package com.example.prison.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.prison.entity.VisitorDeviceInfo;
import com.example.prison.service.VisitorDeviceInfoService;
import com.example.prison.mapper.VisitorDeviceInfoMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author jiang
* @description 针对表【visitor_device_info】的数据库操作Service实现
* @createDate 2024-06-03 15:01:14
*/
@Service
public class VisitorDeviceInfoServiceImpl extends ServiceImpl<VisitorDeviceInfoMapper, VisitorDeviceInfo>
    implements VisitorDeviceInfoService{

    @Autowired
    private VisitorDeviceInfoMapper visitorDeviceInfoMapper;

    @Override
    public PageInfo<VisitorDeviceInfo> get(int pageNumber, int pageSize, String deviceNumber) {
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(visitorDeviceInfoMapper.get(deviceNumber));
    }

    @Override
    public int add(VisitorDeviceInfo vDevice) {
        String deviceNumber = vDevice.getDeviceNumber();
        if (ObjectUtils.isNotEmpty(visitorDeviceInfoMapper.getOneByNumber(deviceNumber))) {
            return -1;
        } else {
            visitorDeviceInfoMapper.add(vDevice);
            return 1;
        }
    }

    @Override
    public void delete(Integer[] ids) {
        visitorDeviceInfoMapper.deleting(ids);
    }

    @Override
    public int update(VisitorDeviceInfo vDevice, Integer deviceId) {
        VisitorDeviceInfo deviceDTO = visitorDeviceInfoMapper.getById(deviceId);
        visitorDeviceInfoMapper.updating(vDevice, deviceId);
        if(visitorDeviceInfoMapper.getListOneByNumber(vDevice.getDeviceNumber()).size() > 1) {
            visitorDeviceInfoMapper.updating(deviceDTO, deviceId);
            return -1;
        }
        visitorDeviceInfoMapper.updating(vDevice, deviceId);
        return 1;
    }

    @Override
    public VisitorDeviceInfo getById(Integer deviceId) {
        return visitorDeviceInfoMapper.getById(deviceId);
    }

    @Override
    public VisitorDeviceInfo getOneByNumber(String deviceNumber) {
        return visitorDeviceInfoMapper.getOneByNumber(deviceNumber);
    }


}




